package alarm.ruiqi.com.alarm_pro.options.base;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ab.fragment.AbProgressFragment;

import alarm.ruiqi.com.alarm_pro.R;

/**
 * Created by 徐宇健 on 2015/6/30 0030.
 */
public abstract class BaseFragment extends AbProgressFragment {

    private View mContentView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        /*if(mContentView != null){
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if(parent!=null)  parent.removeView(mContentView);
        }else {
            mContentView = setAbContentView(inflater,container);
        }*/
        mContentView = setAbContentView(inflater, container);
        return v;
    }

    protected abstract View setAbContentView(LayoutInflater inflater, ViewGroup container);

    public void showContent(View contentView) {
        setContentShown(true);
        setContentView(contentView);
    }


    //region 跳张与设置参数
    protected void setArguments(BaseFragment fragment, Object... params) {
        if (fragment != null) {
            if (params != null) {
                Bundle args = new Bundle();
                args.putSerializable("params", params);
                fragment.setArguments(args);
            } else {
                Log.i(this.getClass().getSimpleName(), "目标ParentFragment要传递的参数为空");
            }
        } else {
            Log.e(this.getClass().getSimpleName(), "目标ParentFragment为空");
        }
    }

    protected void switchFragment(BaseFragment fragment,
                                  String fragmentName, Object... params) {
        switchFragment(R.id.main_content, fragment, fragmentName, params);
    }

    /**
     * Fragment跳转，可返回
     *
     * @param replaceLayout
     * @param fragment
     * @param fragmentName
     * @param params
     */
    protected void switchFragment(int replaceLayout, BaseFragment fragment,
                                  String fragmentName, Object... params) {
        if (fragment != null) {
            if (params != null) {
                Bundle args = new Bundle();
                args.putSerializable("params", params);
                fragment.setArguments(args);
            } else {
                Log.i(this.getClass().getSimpleName(), "目标ParentFragment未传递参数");
            }
        } else {
            Log.e(this.getClass().getSimpleName(), "目标ParentFragment为空");
        }
    }

    protected void switchFragmentNoBack(BaseFragment fragment,
                                        Object... params) {
        switchFragmentNoBack(R.id.main_content, fragment, params);
    }

    /**
     * Fragment跳转，无返回
     *
     * @param replaceLayout
     * @param fragment
     * @param params
     */
    protected void switchFragmentNoBack(int replaceLayout,
                                        BaseFragment fragment, Object... params) {
        if (fragment != null) {
            if (params != null) {
                Bundle args = new Bundle();
                args.putSerializable("params", params);
                fragment.setArguments(args);
            } else {
                Log.i(this.getClass().getSimpleName(), "目标ParentFragment未传递参数");
            }
            android.support.v4.app.FragmentTransaction t = getActivity().getSupportFragmentManager()
                    .beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

            t.replace(replaceLayout, fragment);
            t.commit();
        } else {
            Log.e(this.getClass().getSimpleName(), "目标ParentFragment为空");
        }
    }

    /**
     * 清除Fragment返回栈
     *
     * @param fragmentActivity
     */
    public static void popFragmentBackStack(FragmentActivity fragmentActivity) {
        int count = fragmentActivity.getSupportFragmentManager()
                .getBackStackEntryCount();
        for (int j = 0; j < count; j++) {
            int backStackId = fragmentActivity.getSupportFragmentManager()
                    .getBackStackEntryAt(j).getId();
            fragmentActivity.getSupportFragmentManager().popBackStack(
                    backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }
    //endregion


}
